package baidu.reader.txt.model
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.reader.common.model.VO.*;
    import baidu.reader.doc.model.*;
    import baidu.ui.managers.*;
    import flash.events.*;

    public class TxtChapterModel extends EventDispatcher
    {
        public var isBusy:Boolean = false;
        private var _currentChapterIndex:int = -1;
        private var _chapStartIndex:int;
        private var _chapEndIndex:int;
        private var txtModel:TxtDataModel;
        private var tocModel:TocDataModel;
        private var _currentBlockChapterText:String = " ";
        private var _cumulativeChapterText:String = " ";
        private var _textStrategy:String;
        private var _chapterNotStart:Boolean = false;
        private var _chapterNotEnd:Boolean = false;
        private var _destCharIndex:int = -1;
        private var _useLastPage:Boolean = false;
        private var _blockDelimiterArray:Array;
        private var _chapterStartInBlockArray:Array;
        private var _gotoPage:int;
        private var _gotoOffset:int;
        public static const TOP:String = "text add top";
        public static const BOTTOM:String = "text add bottom";
        public static const REPLACE:String = "text replace";
        public static const NO_ACTION:String = "text no action";

        public function TxtChapterModel(param1:IEventDispatcher = null)
        {
            this._blockDelimiterArray = [];
            this._chapterStartInBlockArray = [];
            super(param1);
            this.initModel();
            return;
        }

        private function initModel() : void
        {
            this.txtModel = InstanceManager.createSingletonInstance(TxtDataModel);
            this.txtModel.addEventListener(Event.COMPLETE, this.dataCompleteHandler);
            this.txtModel.addEventListener(ErrorEvent.ERROR, this.dataErrorHandler);
            this.txtModel.addEventListener(ReaderEvent.TEXT_ADD, this.textAddHandler);
            this.tocModel = InstanceManager.createSingletonInstance(TocDataModel);
            return;
        }

        private function textAddHandler(event:ReaderEvent) : void
        {
            this._textStrategy = String(event.data);
            return;
        }

        public function refresh() : void
        {
            var i:int;
            var currentChapterBlock:int;
            var page:int;
            var currentFirstCharArray:Array;
            var pageStart:int;
            this._isBusy = false;
            this.extractCurrentChapterText();
            switch(this._textStrategy)
            {
                case BOTTOM:
                {
                    this._blockDelimiterArray.push((this._cumulativeChapterText.length + 1));
                    this._cumulativeChapterText = this._cumulativeChapterText + this._currentBlockChapterText;
                    break;
                }
                case TOP:
                {
                    i;
                    while (i < this._blockDelimiterArray.length)
                    {
                        
                        this._blockDelimiterArray[i] = this._blockDelimiterArray[i] + this._currentBlockChapterText.length;
                        i = (i + 1);
                    }
                    this._cumulativeChapterText = this._currentBlockChapterText + this._cumulativeChapterText;
                    this._blockDelimiterArray.unshift(0);
                    break;
                }
                case REPLACE:
                {
                    this._cumulativeChapterText = this._currentBlockChapterText;
                    this._blockDelimiterArray = [0];
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this._destCharIndex < 0)
            {
            }
            dispatchEvent(new Event(Event.COMPLETE));
            return;
        }

        private function extractCurrentChapterText() : void
        {
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            if (this._currentChapterIndex == -1)
            {
                return;
            }
            var _loc_1:* = this.tocModel.tocData[this._currentChapterIndex];
            var _loc_2:* = this.txtModel.pageFirstCharArray;
            var _loc_3:* = _loc_1.page - this.txtModel.currentBlock * this.txtModel.default_rn;
            this._chapStartIndex = _loc_2[_loc_3] + _loc_1.offset;
            var _loc_4:* = _loc_1.page - this.txtModel.blockCacheStart * this.txtModel.default_rn;
            if (this._chapStartIndex == 0 && _loc_4 < 0)
            {
                this._chapterNotStart = true;
            }
            else
            {
                this._chapterNotStart = false;
            }
            if (this._currentChapterIndex == (this.tocModel.tocData.length - 1) || this.txtModel.currentDocid != this.tocModel.tocData[(this._currentChapterIndex + 1)].docid)
            {
                this._chapEndIndex = this.txtModel.currentBlockTextData.length;
                _loc_5 = Math.ceil(this.txtModel.totalPage / this.txtModel.default_rn);
                if (this.txtModel.blockCacheEnd < (_loc_5 - 1))
                {
                    this._chapterNotEnd = true;
                }
                else
                {
                    this._chapterNotEnd = false;
                }
            }
            else
            {
                _loc_6 = this.tocModel.tocData[(this._currentChapterIndex + 1)].page - this.txtModel.currentBlock * this.txtModel.default_rn;
                _loc_7 = this.tocModel.tocData[(this._currentChapterIndex + 1)].page - this.txtModel.blockCacheEnd * this.txtModel.default_rn;
                if (_loc_7 > this.txtModel.default_rn)
                {
                    this._chapterNotEnd = true;
                }
                else
                {
                    this._chapterNotEnd = false;
                }
                if (_loc_6 > this.txtModel.default_rn)
                {
                    this._chapEndIndex = this.txtModel.currentBlockTextData.length;
                }
                else
                {
                    this._chapEndIndex = _loc_2[_loc_6] + this.tocModel.tocData[(this._currentChapterIndex + 1)].offset;
                }
            }
            this._currentBlockChapterText = this.txtModel.currentBlockTextData.substring(this._chapStartIndex, this._chapEndIndex);
            switch(this._textStrategy)
            {
                case BOTTOM:
                {
                    this._chapterStartInBlockArray.push(this._chapStartIndex);
                    break;
                }
                case TOP:
                {
                    this._chapterStartInBlockArray.unshift(this._chapStartIndex);
                    break;
                }
                case REPLACE:
                {
                    this._chapterStartInBlockArray = [this._chapStartIndex];
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._currentBlockChapterText = trimEnter(this._currentBlockChapterText);
            if (this._currentBlockChapterText == "")
            {
                this._currentBlockChapterText = " ";
            }
            return;
        }

        private function dataCompleteHandler(event:Event) : void
        {
            this.refresh();
            return;
        }

        private function dataErrorHandler(event:Event) : void
        {
            this._isBusy = false;
            return;
        }

        public function gotoChapter(param1:int) : void
        {
            if (this._isBusy)
            {
                return;
            }
            var _loc_2:* = this.tocModel.tocData[param1];
            this.gotoTextPosition(_loc_2.docid, _loc_2.page, _loc_2.offset);
            return;
        }

        public function gotoNextChapter() : Boolean
        {
            if (this._currentChapterIndex >= (this.tocModel.tocData.length - 1))
            {
                return false;
            }
            this.gotoChapter((this._currentChapterIndex + 1));
            return true;
        }

        public function gotoPreviousChapter() : Boolean
        {
            var _loc_1:TocItemVO = null;
            if (this._currentChapterIndex <= 0)
            {
                return false;
            }
            _loc_1 = this.tocModel.tocData[(this._currentChapterIndex - 1)];
            this.gotoChapterLastPage(_loc_1);
            return true;
        }

        private function gotoChapterLastPage(param1:TocItemVO) : void
        {
            var docid:String;
            var lastPage:int;
            var infoProxy:DocIdInfoProxy;
            var tocItem:* = param1;
            this._useLastPage = true;
            docid = tocItem.docid;
            if (docid == this.txtModel.currentDocid)
            {
                lastPage = (this.tocModel.tocData[this._currentChapterIndex].page - 1);
                if (lastPage < 1)
                {
                    lastPage;
                }
                this.gotoTextPosition(docid, lastPage, 0);
            }
            else
            {
                infoProxy = InstanceManager.createSingletonInstance(DocIdInfoProxy);
                infoProxy.getInfo(docid);
                infoProxy.addEventListener(Event.COMPLETE, function (event:Event) : void
            {
                var _loc_2:* = infoProxy.totalPage;
                gotoTextPosition(docid, _loc_2, 0);
                return;
            }
            );
            }
            return;
        }

        public function gotoTextPosition(param1:String, param2:int, param3:int) : void
        {
            if (this._isBusy)
            {
                return;
            }
            this._isBusy = true;
            this.determineCurrentChapter(param1, param2, param3);
            this._gotoPage = param2;
            this._gotoOffset = param3;
            this.txtModel.loadPage(param1, param2);
            return;
        }

        private function determineCurrentChapter(param1:String, param2:int, param3:int = 0) : void
        {
            var _loc_4:TocItemVO = null;
            var _loc_5:int = 0;
            while (_loc_5 < (this.tocModel.tocData.length - 1))
            {
                
                _loc_4 = this.tocModel.tocData[_loc_5];
                if (param1 == _loc_4.docid)
                {
                    trace(param2 >= _loc_4.page && param2 < this.tocModel.tocData[(_loc_5 + 1)].page);
                    trace(param2 == _loc_4.page && param2 == this.tocModel.tocData[(_loc_5 + 1)].page && param3 < this.tocModel.tocData[(_loc_5 + 1)].offset);
                    if (this.tocModel.tocData[(_loc_5 + 1)].docid != param1)
                    {
                        break;
                    }
                    else if (param2 >= _loc_4.page && param2 < this.tocModel.tocData[(_loc_5 + 1)].page || param2 == _loc_4.page && param2 == this.tocModel.tocData[(_loc_5 + 1)].page && param3 < this.tocModel.tocData[(_loc_5 + 1)].offset)
                    {
                        break;
                    }
                }
                _loc_5++;
            }
            if (this._currentChapterIndex != _loc_5 && this._currentChapterIndex != -1)
            {
                this.txtModel.reset();
            }
            this._currentChapterIndex = _loc_5;
            return;
        }

        public function loadChapterContentNext() : void
        {
            if (this._isBusy)
            {
                return;
            }
            if (!this._chapterNotEnd)
            {
                return;
            }
            this.txtModel.loadNextBlock();
            this._gotoPage = -1;
            this._gotoOffset = -1;
            this._isBusy = true;
            return;
        }

        public function loadChapterContentPrevious() : void
        {
            if (this._isBusy)
            {
                return;
            }
            if (!this._chapterNotStart)
            {
                return;
            }
            this.txtModel.loadPreviousBlock();
            this._gotoPage = -1;
            this._gotoOffset = -1;
            this._isBusy = true;
            return;
        }

        public function get textStrategy() : String
        {
            return this._textStrategy;
        }

        public function get currentChapterText() : String
        {
            return this._currentBlockChapterText;
        }

        public function get cumulativeChapterText() : String
        {
            return this._cumulativeChapterText;
        }

        public function get destCharIndex() : int
        {
            return this._destCharIndex;
        }

        private function get _isBusy() : Boolean
        {
            return this.isBusy;
        }

        private function set _isBusy(param1:Boolean) : void
        {
            this.isBusy = param1;
            return;
        }

        public function get chapStartIndex() : int
        {
            return this._chapStartIndex;
        }

        public function get chapEndIndex() : int
        {
            return this._chapEndIndex;
        }

        public function get blockDelimiterArray() : Array
        {
            return this._blockDelimiterArray;
        }

        public function get chapterStartInBlockArray() : Array
        {
            return this._chapterStartInBlockArray;
        }

        public function get useLastPage() : Boolean
        {
            var _loc_1:* = this._useLastPage;
            this._useLastPage = false;
            return _loc_1;
        }

        public function get chapterNotEnd() : Boolean
        {
            return this._chapterNotEnd;
        }

        public function get currentChapterIndex() : int
        {
            return this._currentChapterIndex;
        }

        public function get totalChapter() : int
        {
            return this.tocModel.tocData.length;
        }

        public static function trimEnter(param1:String) : String
        {
            while (/\n$/g.test(param1))
            {
                
                param1 = param1.replace(/\n$/g, "");
            }
            return param1;
        }

    }
}
